package com.tj.collection.db.admin.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.utils.CollectionUtils;
import com.tj.collection.common.old.BaseDAOImpl;
import com.tj.collection.db.admin.dao.AdminDAO;
import com.tj.collection.db.admin.entity.AdminEntity;
import com.tj.collection.db.admin.entity.AdminEntityConditions;
import com.tj.collection.db.admin.dao.mp.AdminEntityMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/10
 * Time: 下午5:25
 */
@Repository
public class AdminDAOImpl extends BaseDAOImpl<AdminEntity, Long, AdminEntityConditions> implements AdminDAO {

    @Autowired
    protected AdminEntityMapper adminEntityMapper;

    @Autowired
    @Override
    public void setBaseMapper() {
        super.setBaseMapper(adminEntityMapper);
    }

    @Override
    public List<AdminEntity> getByMobile(String mobile) {
        AdminEntityConditions conditions = new AdminEntityConditions();
        AdminEntityConditions.Criteria criteria = conditions.createCriteria();
        criteria.andMobileEqualTo(mobile);

        List<AdminEntity> adminEntityList = adminEntityMapper.selectByExample(conditions);
        if (CollectionUtils.isNotEmpty(adminEntityList)) {
            return adminEntityList;
        }
        return new ArrayList<>();
    }

    @Override
    public List<AdminEntity> getByMerchantCodeAndMobile(String merchantCode, String mobile) {
        AdminEntityConditions conditions = new AdminEntityConditions();
        AdminEntityConditions.Criteria criteria = conditions.createCriteria();
        criteria.andMobileEqualTo(mobile);
        criteria.andMerchantCodeEqualTo(merchantCode);
        List<AdminEntity> adminEntityList = adminEntityMapper.selectByExample(conditions);
        if (CollectionUtils.isNotEmpty(adminEntityList)) {
            return adminEntityList;
        }
        return new ArrayList<>();
    }

    /**
     * @param adminId
     * @return
     */
    @Override
    public AdminEntity getByAdminId(long adminId) {
        AdminEntity adminEntity = adminEntityMapper.selectByPrimaryKey(adminId);
        return adminEntity;
    }

    @Override
    public AdminEntity getByAccount(String account) {
        AdminEntityConditions conditions = new AdminEntityConditions();
        AdminEntityConditions.Criteria criteria = conditions.createCriteria();
        criteria.andAccountEqualTo(account);

        List<AdminEntity> adminEntityList = adminEntityMapper.selectByExample(conditions);
        if (CollectionUtils.isNotEmpty(adminEntityList)) {
            return adminEntityList.get(0);
        }

        return null;
    }

    @Override
    public List<AdminEntity> getListByAccountAndName(String account, String name, Page page, String merchantCode) {
        AdminEntityConditions conditions = new AdminEntityConditions();
        AdminEntityConditions.Criteria criteria = conditions.createCriteria();
        if (StringUtils.isNotEmpty(account)) {
            criteria.andAccountLike("%" + account + "%");
        }

        if (StringUtils.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andMerchantCodeEqualTo(merchantCode);
        conditions.setPage(page);
        page.setTotalCount(countByExample(conditions));
        return adminEntityMapper.selectByExample(conditions);
    }

    /**
     * 获取当前系统所有的信审人员
     *
     * @return
     */
    @Override
    public List<AdminEntity> getAllAuditor(List<Long> auditorIdList, Long status) {
        AdminEntityConditions conditions = new AdminEntityConditions();
        AdminEntityConditions.Criteria criteria = conditions.createCriteria();
        if (CollectionUtils.isNotEmpty(auditorIdList)) {
            criteria.andIdIn(auditorIdList);
        }
        criteria.andStatusEqualTo(status);
        criteria.andDeletedEqualTo(Boolean.FALSE);
        return adminEntityMapper.selectByExample(conditions);
    }

    /**
     * 获取当前系统所有的信审人员
     *
     * @param auditorIdList
     * @param status
     * @param merchantCode
     * @return
     */
    @Override
    public List<AdminEntity> getAllAuditor(List<Long> auditorIdList, Long status, String merchantCode) {
        AdminEntityConditions conditions = new AdminEntityConditions();
        AdminEntityConditions.Criteria criteria = conditions.createCriteria();
        if (CollectionUtils.isNotEmpty(auditorIdList)) {
            criteria.andIdIn(auditorIdList);
        }
        criteria.andStatusEqualTo(status);
        criteria.andMerchantCodeEqualTo(merchantCode);
        criteria.andDeletedEqualTo(Boolean.FALSE);
        return adminEntityMapper.selectByExample(conditions);
    }

    @Override
    public int updateVersionCode(Integer adminId, Integer versionCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", adminId);
        params.put("versionCode", versionCode);
        return adminEntityMapper.updateVersionCode(params);
    }
}
